//
//  LZSectedPicVC.m
//  DoctorFAQs
//
//  Created by langZai on 2017/3/28.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZSectedPicVC.h"
#import "ZHealthModel.h"
#import "ZRecordTableViewCell.h"
#import "LZSelecPicListVC.h"
#import "SPPlaceholderCell.h"
static NSString *const placeCellID = @"placeholders";
static NSString * const KZRecordIdentifier = @"LZRecordIdentifier";


@interface LZSectedPicVC ()<UITableViewDelegate,UITableViewDataSource>{
    LZUserInfo *userInfo;
}
@property (nonatomic,strong) UITableView * mainTableView;
/** 健康档案模型数组 */
@property (nonatomic, strong) NSMutableArray <ZHealthModel *>*dataArray;
@property (nonatomic,assign) BOOL isHaveData;

@end

@implementation LZSectedPicVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initialzeDatas];
    [self initlizeInterface];
    [_mainTableView.mj_header beginRefreshing];
}

- (void)initialzeDatas {
    self.navigationItem.title = @"健康档案";
    userInfo = [LZUserInfo getUserInfo];
    _dataArray = [NSMutableArray array];
    self.isHaveData = YES;
}
- (void)initlizeInterface {
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.mainTableView];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    __weak typeof(self) weakSelf = self;
    // 下拉刷新
    self.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf pullDownData];
        [weakSelf.mainTableView.mj_header endRefreshing];
    }];
}

//表格视图协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.isHaveData ? self.dataArray.count : 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHaveData == YES) {
        return 95*ysIZE;
    }else {
        return  SCREEN_Height-104;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHaveData == YES) {
        ZRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:KZRecordIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.heathModel = _dataArray[indexPath.row];
        return  cell;
    }else {
        SPPlaceholderCell *cell = [tableView dequeueReusableCellWithIdentifier:placeCellID];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LZLog(@"点击");
    if (self.isHaveData == NO) {
        return;
    }
        
    LZSelecPicListVC *selectPicDetailVC = [[LZSelecPicListVC alloc] init];
    selectPicDetailVC.fileID = _dataArray[indexPath.row].fileID;
    selectPicDetailVC.selectIndex = self.selectIndex;
    [self.navigationController pushViewController:selectPicDetailVC animated:YES];
}


- (UITableView *)mainTableView {
    if (_mainTableView) {
        return _mainTableView;
    }
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _mainTableView.dataSource = self;
    _mainTableView.delegate = self;
//    _mainTableView.rowHeight = 95*ysIZE;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTableView.backgroundColor = MYGRAYCOLOR;
    [_mainTableView registerClass:[ZRecordTableViewCell class] forCellReuseIdentifier:KZRecordIdentifier];
    [_mainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPPlaceholderCell class]) bundle:nil] forCellReuseIdentifier:placeCellID];
    return _mainTableView;
}

// 下拉刷新方法
- (void)pullDownData {
    __weak typeof(self) weakSelf = self;
    NSDictionary *paramas = @{@"uid":userInfo.uid};
    self.isHaveData = YES;
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"File/index" sucessBlock:^(id content) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"-------选择照片健康档案首页数据------%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [weakSelf dealPullDownDataWithDict:dict];
        }else if ([dict[@"code"] intValue] == 0) {
            self.isHaveData = NO;
            [self.mainTableView reloadData];
            
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
    } failure:^(NSError *error) {
            self.isHaveData = NO;
        [self.mainTableView reloadData];
        [YJProgressHUD showMessage:@"加载失败" inView:self.view];
    }];
}


// 处理下拉刷新以后的数据
- (void)dealPullDownDataWithDict:(NSDictionary *)dict {
//    [_dataArray removeAllObjects];
//    NSArray *tempArr = dict[@"list"];
//    if (tempArr.count == 0) {
//        self.isHaveData = NO;
//        [YJProgressHUD showMessage:@"没有更多数据了~" inView:self.view];
//    }
//    for (NSDictionary *dict in tempArr) {
//        ZHealthModel *healthModel = [[ZHealthModel alloc] initWithDict:[dict mutableCopy]];
//        [_dataArray addObject:healthModel];
//    }
//    [_mainTableView reloadData];
    [self.mainTableView.mj_footer resetNoMoreData];
    [_dataArray removeAllObjects];
    NSArray *tempArr = dict[@"list"];
    if (tempArr.count == 0) {
        self.isHaveData = NO;
        [YJProgressHUD showMessage:@"没有更多数据了~" inView:self.view];
        [self.mainTableView.mj_footer endRefreshingWithNoMoreData];
    }else if (tempArr.count < 10) {
        [self.mainTableView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSDictionary *dict in tempArr) {
        ZHealthModel *healthModel = [[ZHealthModel alloc] initWithDict:[dict mutableCopy]];
        [_dataArray addObject:healthModel];
    }
    [self.mainTableView reloadData];
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
  }

@end
